package com.mine.yyb.modules.sys.utils;

import java.io.File;
import java.text.MessageFormat;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;

import com.mine.yyb.common.consts.YybConstants;
import com.mine.yyb.common.utils.email.MineMailSender;
import com.mine.yyb.common.utils.email.MineMailVO;
import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.modules.sys.enums.SysCodeEnum;

/**
 * <pre>
 * <一句话功能简述>
 * 邮件工具类
 * <功能详细描述>
 * 活动、广告发送邮件
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2015年12月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Component
public class CmsMail
{
    /*** 日志 */
    private static Logger logger = LoggerFactory.getLogger(CmsMail.class);
    
    // 实例化邮件信息类
    @Autowired
    private MineMailVO mineMailVO;
    
    @Autowired
    private MineMailSender mineMailSender;
    
    public static Document document;
    
    /**
     * 读取xml文件
     */
    static
    {
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            document = db.parse(new File(YybConstants.ROOT_PATH + YybConstants.CONFIG_PATH + File.separator + "yyb"
                + File.separator + "email.xml"));
        }
        catch (Exception e)
        {
            logger.error(SysCodeEnum.ERROR_SYS_PARSE_EMAIL_CONFIG_FAIL.name(), e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 发送注册激活邮件
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年7月7日]
     * @param mode 类型
     * @param emailAddress 邮件地址
     * @param name 发送邮件名
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    public void sendActiveMail(String emailAddress, String name, Map<String, String> params)
        throws MineException
    {
        try
        {
            String innerID = params.get("innerID");
            String username = params.get("username");
            
            mineMailVO.setEmailAddress(emailAddress);
            mineMailVO.setEmailSubject(MessageFormat.format(document.getElementsByTagName("subject").item(0).getFirstChild().getNodeValue(), name));
            mineMailVO.setEmailContent(MessageFormat.format(document.getElementsByTagName("context").item(0).getFirstChild().getNodeValue(), innerID, username));
            mineMailSender.sendHtmlMail(mineMailVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    public void sendRecoveryMail(String emailAddress, String name, Map<String, String> params)
        throws MineException
    {
        try
        {
            String password = params.get("password");
            
            mineMailVO.setEmailAddress(emailAddress);
            mineMailVO.setEmailSubject(document.getElementsByTagName("subject").item(1).getFirstChild().getNodeValue());
            mineMailVO.setEmailContent(MessageFormat.format(document.getElementsByTagName("context").item(1).getFirstChild().getNodeValue(), password));
            mineMailSender.sendHtmlMail(mineMailVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
}